/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;


import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Phylum;
import muhnes.sizoo.HibernateUtilZoologia;
/**
 *
 * @author Ricardo Orellana
 */
public class PhylumDaoImpl implements PhylumDao{
    
    Session sesion;
    
    public PhylumDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }
    
    @Override
    public void guardar( Phylum phylum ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( phylum );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Phylum > filos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( filos );
        transaccion.commit();
    }

    @Override
    public void actualizar( Phylum phylum ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( phylum );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Phylum > filos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( filos );
        transaccion.commit();
    }

    @Override
    public void eliminar( Phylum phylum ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( phylum );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Phylum > filos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( filos );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Phylum phylum = buscarPorId( id );
        sesion.delete( phylum );
        transaccion.commit();
    }

    @Override
    public Phylum buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Phylum where CodigoPhylum = " + id);
        Phylum phylum = (Phylum)consulta.uniqueResult();
        transaccion.commit();
        return phylum;
    }

    @Override
    public List< Phylum > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Phylum");
        List< Phylum > filos = consulta.list();
        transaccion.commit();
        return filos;
    }
   
    
}
